﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using S7.Net;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows.Input;
using 小玩意.Comm;
using 小玩意.Model;

namespace 小玩意.ViewModel
{
    public partial class S7CommViweModel : ObservableObject
    {
        public ICommand ConnCommand { get; }


        //private string _addressValue;

        //public string AddressValue
        //{
        //    get { return _addressValue; }
        //    set
        //    {
        //        if (_addressValue != value)
        //        {
        //            _addressValue = value;
        //            OnPropertyChanged(nameof(AddressValue));
        //        }
        //    }
        //}

        private string _addressValue;

        public string AddressValue { get => _addressValue; set => SetProperty(ref _addressValue, value); }

        private string _rack;
        public string Rack { get => _rack; set => SetProperty(ref _rack, value); }

        private string _slot;
        public string Slot { get => _slot; set => SetProperty(ref _slot, value); }
        private CpuType _cpuType;
        public CpuType CpuType { get => _cpuType; set => SetProperty(ref _cpuType, value); }
        public ObservableCollection<S7Model> myData { get; set; }
        public ObservableCollection<S7Model> MyData
        {
            get { return myData; }
            set
            {
                if (myData != value)
                {
                    myData = value;
                    OnPropertyChanged(nameof(MyData));
                }
            }
        }


        Siemens Siemens;
        public S7CommViweModel()
        {
            ConnCommand = new RelayCommand(S7ConnCommand);
            MyData = InitDevice.GetPlcDevice();
            S7ConnCommand();
        }
       
        /// <summary>
        /// 与PLC通讯
        /// </summary>
        private void S7ConnCommand()
        {
            string address;
            short rack;
            short slot;

            if (_addressValue != null)
            {
                address = _addressValue;
                rack = Convert.ToInt16(_rack);
                slot = Convert.ToInt16(_slot);

                Siemens = new Siemens(_cpuType, address, rack, slot);
                //Siemens.WritePlcInt32(_addressValue, "22");
                //Siemens.ReadPlc(11, "");
                List<string > strings = new List<string>();
                List<string> strings1 = new List<string>();
                List<string> strings2 = new List<string>();
                List<string> strings3 = new List<string>();
                strings.Add("1");
                strings.Add("8");
                strings1.Add("3");
                strings1.Add("47");
                strings2.Add("52");
                strings2.Add("6");
                strings3.Add("72");
                strings3.Add("7");
                Dictionary<Siemens.Type, List<string>> keyValuePairs = new Dictionary<Siemens.Type, List<string>>();
                keyValuePairs.Add(Siemens.Type.Bool, strings);
                keyValuePairs.Add(Siemens.Type.Int16, strings1);
                keyValuePairs.Add(Siemens.Type.Int32, strings2);
                keyValuePairs.Add(Siemens.Type.String, strings3);
                Siemens.GetAllPlcDataAddress(keyValuePairs);
            }
            else
            {
                ErrorViewModel.Errornotice("PLC地址不能为空！", true);
            }




        }

    }
}
